from asyncio import Queue
import asyncio

async def add_task(queue: Queue, tasks):
    for task in tasks:
        await queue.put(task)


async def deal_task(queue: Queue, count: int):
    for _ in range(count):
        if queue.empty():
            break
        task = await queue.get()
        try:
            task()
        except Exception as e:
            print(e)
        finally:
            queue.task_done()


async def main():
    q = Queue(5)
    count = 3
    tasks = [lambda i=i: print(i) for i in range(count)]
    await add_task(q, tasks)
    await deal_task(q, count)
    await q.join()

asyncio.run(main())
